import { defineStore } from 'pinia'

export const useTodoStore = defineStore('todo',{
  state(){
    return{
      // 初始化
      todoList:[],
      filterType:'0'
    }
  },
  getters:{
    // 实现切换 显示不同内容
    filteredTodoList(state){
      if (state.filterType === '1'){
        return state.todoList.filter((item)=>item.isFinished === false)
      } else if (state.filterType === '2'){
        return state.todoList.filter((item)=>item.isFinished === true)
      } else {
        return state.todoList
      }
    }
  },
  actions:{
    // 绑定添加的点击事件
    addTodo(content){
      this.todoList.push({
        id:Date.now(),
        content,
        isFinished:false
      })
    },
    // 返回 isFinished = true 的元素 绑定完成按钮的点击事件
    finishTodo(id){
      this.todoList.find((item)=>item.id === id).isFinished = true
    },
    // 删除元素
    removeTodo(id){
      const index = this.todoList.findIndex((item)=>item.id === id)
      this.todoList.splice(index,1)
    },
    // 更新type
    setFilterType(type){
      this.filterType = type
    }
  }
})
